<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/17
 * Time: 8:42
 */

namespace app\admin\model;


use think\Model;
use think\Db;

class DingdanModel extends Model
{
    // 确定链接表名
    protected $name = 'dingdan';

    /**
     * 查询订单
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getOrderByWhere($where,$offset=0, $limit=10)
    {

        return  $this->where($where)->limit($offset, $limit)->select();


    }

    /**
     * 根据搜索条件获取所有的订单数量
     * @param $where
     */
    public function getAllOrder($where)
    {
        return  $this->where($where)->count();
    }


    //下单时间
    public function getAddtimeAttr($v){
        return date('Y-m-d H:i:s',$v);
    }
    //服务时间
    public function getSevriceTimeAttr($v){
        return date('Y-m-d',$v);
    }

    public function getStatusAttr($v){
        $msg = config('order_status');

        return $msg[$v];
    }

    /**
     * 添加订单
     * @param $param
     */
    public function addOrderle($param)
    {
        try{
            $result = $this->validate('OrderleValidate')->save($param);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{

                return msg(1, url('articles/index'), '添加订单成功');
            }
        }catch (\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑订单信息
     * @param $param
     */
    public function editOrder($param)
    {
        Db::startTrans();
        try{

            $result = $this->where('id',$param['oderid'])->update(['sevrice_id'=>$param['sevrice_id'],'status'=>2]);

            if(false === $result){
                // 验证失败 输出错误信息
                throw new \Exception('指派失败');
            }else{
                return msg(1, url('articles/index'), '指派成功');
            }
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 根据订单的id 获取订单的信息
     * @param $id
     */
    public function getOneOrderle($id)
    {
        return $this->where('id', $id)->find();
    }

    /**
     * 删除订单
     * @param $id
     */
    public function delOrderle($id)
    {
        try{

            $this->where('id', $id)->delete();
            return msg(1, '', '删除订单成功');

        }catch(\Exception $e){
            return msg(-1, '', $e->getMessage());
        }
    }

}